<template>
  <div id="login" v-title data-title="登录 - 博乐阁">


    <div class="me-login-box me-login-box-radius">
      <h1>博乐阁 登录</h1>

      <el-form ref="userForm" :model="userForm" :rules="rules">
        <el-form-item prop="account">
          <el-input placeholder="用户名" v-model="userForm.account"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input placeholder="密码" type="password" v-model="userForm.password"></el-input>
        </el-form-item>

        <el-form-item prop="code">
          <div class="bind_code margin_top">
            <el-input
              class="bind_code_input code"
              v-model="userForm.code"
              type="text"
              placeholder="请输入验证码"
            />
            <el-button
              @click="doSendCode"
              class="bind_code_gain"
              :disabled="sendObj.isSend"
            >{{ sendObj.sendCodeText }}
            </el-button
            >
          </div>
        </el-form-item>

        <el-form-item size="small" class="me-login-button">
          <el-button type="primary" @click.native.prevent="login('userForm')">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {loginSendCode} from "../api/login";

export default {
  name: 'Login',
  data() {
    return {
      userForm: {
        account: '',
        password: '',
        tempToken: '',
        code: ''
      },
      sendObj: {
        sendInterval: 60,
        sendCodeText: '发送邮箱验证码',
        isSend: false,
        timer: null
      },
      rules: {
        account: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {max: 10, message: '不能大于10个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {max: 10, message: '不能大于10个字符', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
          {min: 6, max: 6, message: '请输入6位验证码', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    login(formName) {
      let that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          that.$store.dispatch('login', that.userForm).then(() => {
            that.$router.go(-1)
          }).catch((error) => {
            if (error !== 'error') {
              that.$message({message: error, type: 'error', showClose: true});
            }
          })
        } else {
          return false;
        }
      });
    },
    // 发送验证码
    async doSendCode() {
      // 合法性校验
      if (this.userForm.account === '' || this.userForm.account.length > 10) {
        this.$message({
          message: '用户名不符合要求',
          type: 'warning'
        })
        return
      }
      // 发送验证码
      let resp = await loginSendCode(this.userForm.account)
      if (!resp.success || resp.data === '') {
        this.$message({
          message: '验证码发送失败，可能是因为用户不存在',
          type: 'warning'
        })
        return
      }
      // 保存临时令牌
      this.userForm.tempToken = resp.data
      // 修改页面样式
      this.sendObj.sendCodeText = this.sendObj.sendInterval
      this.sendObj.isSend = true
      clearInterval(this.sendObj.timer)
      this.sendObj.timer = setInterval(() => {
        if (this.sendObj.sendCodeText === 0) {
          clearInterval(this.sendObj.timer)
          this.sendObj.sendCodeText = '发送验证码'
          this.sendObj.isSend = false
          this.userForm.tempToken = ''
          return
        }
        let time
        try {
          time = parseInt(this.sendObj.sendCodeText)
        } catch (e) {
          time = 60
        }
        this.sendObj.sendCodeText = time - 1
      }, 1000)
    }
  }
}
</script>

<style scoped>
#login {
  min-width: 100%;
  min-height: 100%;
}

.me-video-player {
  background-color: transparent;
  width: 100%;
  height: 100%;
  object-fit: fill;
  display: block;
  position: absolute;
  left: 0;
  z-index: 0;
  top: 0;
}

.me-login-box {
  position: absolute;
  width: 300px;
  height: 280px;
  background-color: white;
  margin-top: 150px;
  margin-left: -180px;
  left: 50%;
  padding: 30px;
}

.me-login-box-radius {
  border-radius: 10px;
  box-shadow: 0px 0px 1px 1px rgba(161, 159, 159, 0.1);
}

.me-login-box h1 {
  text-align: center;
  font-size: 24px;
  margin-bottom: 20px;
  vertical-align: middle;
}

.me-login-design {
  text-align: center;
  font-family: 'Open Sans', sans-serif;
  font-size: 18px;
}

.me-login-design-color {
  color: #5FB878 !important;
}

.me-login-button {
  text-align: center;
}

.me-login-button button {
  width: 100%;
}

/* ------------------------------- */
.bind_code {
  position: relative;
}

/*.code /deep/ .el-input__suffix {*/
/*  right: 97px;*/
/*}*/

.bind_code_gain {
  border: 0;
  background: none;
  padding: 0 0 0 10px;
  border-radius: 0;
  position: absolute;
  top: calc(50% - 9px);
  right: 10px;
  font-size: 14px;
  line-height: 18px;
  cursor: pointer;
  border-left: 1px solid #d8d8d8;
}


</style>
